import { checkPwdInit, getBaseInfo } from "@src/api/member";
import router from "@src/router";

const QR = require("./weapp-qrcode"); //引入weapp-qrcode
export const getWriteOffQrCode = (writeOffCode) => {
  return new Promise((resolve, reject) => {
    const params = writeOffCode; // 二维码参数
    const imgData = QR.drawImg(params, {
      typeNumber: 4, // 密度
      errorCorrectLevel: "L", // 纠错等级
      size: 800, // 白色边框
    });
    if (imgData) {
      resolve(imgData);
    } else {
      reject(false);
    }
  });
};
export const copyClipboard = (val) => {
  wx.setClipboardData({
    data: val,
    success: function (res) {
      wx.getClipboardData({
        success: function (res) {
          wx.showToast({
            title: "复制成功",
          });
        },
      });
    },
  });
};

export const getPayChannels = (payChannelsStr) => {
  let tempList = payChannelsStr.split(",");
  let payChannels = [];
  for (let i = 0; i < tempList.length; i++) {
    if (tempList[i] === "0") {
      payChannels.push("wx");
    } else if (tempList[i] === "1") {
      payChannels.push("balance");
    }
  }
  return payChannels;
};

export const handleCheckAllowedPwd = (goodPrice, accountPayRule) => {
  return new Promise(async (resolve, reject) => {
    const baseInfo = await getBaseInfo();
    let initial = false;
    const { memberCategoryId } = baseInfo.data;
    if (memberCategoryId) {
      let {
        data: { initialed },
      } = await checkPwdInit();
      initial = initialed;
    }
    if (
      memberCategoryId &&
      goodPrice > accountPayRule.skipPwdAmount / 1000 &&
      !initial
    ) {
      reject(false);
      router.push({
        name: "member/resetPassword",
        params: { type: "init" },
      });
    }
    {
      resolve(true);
    }
  });
};

export const resolveTimeAddZero = (time) => {
  return time < 10 ? "0" + time : time;
};

export const parseTime = (time) => {
  const date = time ? new Date(time) : new Date();
  const getFullYear = date.getFullYear();
  const getMonth =
    date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1;
  const getDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  const getHours =
    date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  const getMinutes =
    date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  // const formatObj = `${getFullYear}-${getMonth}-${getDate} ${getHours}:${getMinutes}`;
  return {
    getFullYear,
    getMonth,
    getDate,
    getHours,
    getMinutes,
  };
};
